package cn.zhou.student.service.impl;


import cn.zhou.student.bean.PageBean;
import cn.zhou.student.bean.Student;
import cn.zhou.student.bean.pageCondition;
import cn.zhou.student.dao.StudentMapper;
import cn.zhou.student.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;


@Service
public class StudentServiceImp implements StudentService {
    @Autowired
    private StudentMapper mapper;
    @Autowired
    private RedisTemplate redisTemplate;
    public List getAll(){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.like("name","李%");

        List<Student> students = mapper.selectList(studentQueryWrapper);
        return students;
    }

    @Override
    public PageBean getConditionPageStudent(pageCondition pageCondition,String currentPage) {
//        //redis获取数据
//        PageBean<Student> studentPageBean  = (PageBean)redisTemplate.opsForValue().get("student:" + currentPage);
//        if(StringUtils.isEmpty(pageCondition.getDept())&&StringUtils.isEmpty(pageCondition.getName())
//                &&StringUtils.isEmpty(pageCondition.getSubject())
//                &&StringUtils.isEmpty(pageCondition.getId())&&studentPageBean!=null){
//            return studentPageBean;
//        }
        PageBean<Student> studentPageBean=new PageBean<Student>();
        //创建mybatis分页对象
        Page<Student> studentPage = new Page<Student>(pageCondition.getCurrentPage(),studentPageBean.getNum());
        //创建条件对象
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        if(pageCondition.getId()!=null&&pageCondition.getId()!=""){
            studentQueryWrapper.eq("id",pageCondition.getId());
        }
        if(pageCondition.getName()!=null&&pageCondition.getName()!=""){
//            studentQueryWrapper.eq("name",pageCondition.getName());
            studentQueryWrapper.like("name",pageCondition.getName());
        }
        if(pageCondition.getSubject()!=null&&pageCondition.getSubject()!=""){
//            studentQueryWrapper.eq("subject",pageCondition.getSubject());
            studentQueryWrapper.like("subject",pageCondition.getSubject());
        }
        if(pageCondition.getDept()!=null&&pageCondition.getDept()!=""){
//            studentQueryWrapper.eq("dept",pageCondition.getDept());
            studentQueryWrapper.like("dept",pageCondition.getDept());

        }
        //查询
        mapper.selectPage(studentPage, studentQueryWrapper);
        //封装结果集对象
        studentPageBean.setCurrentPage(studentPage.getCurrent());
        studentPageBean.setAllPage(studentPage.getPages());
        studentPageBean.setAll(studentPage.getTotal());
        studentPageBean.setResults(studentPage.getRecords());
//        if(StringUtils.isEmpty(pageCondition.getDept())&&StringUtils.isEmpty(pageCondition.getName())&&StringUtils.isEmpty(pageCondition.getSubject())
//                &&StringUtils.isEmpty(pageCondition.getId())&&studentPageBean.getResults().size()!=0){
//            redisTemplate.opsForValue().set("student:"+currentPage,studentPageBean);
//        }


        return studentPageBean;

    }

    @Override
    public void addStudent(Student student) {
        int insert = mapper.insert(student);
//        if(insert>0){
//            Set keys = redisTemplate.keys("*");
//            redisTemplate.delete(keys);
//        }
    }

    @Override
    public Student getById(String id) {
        Student student = mapper.selectById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        mapper.updateById(student);
//        Set keys = redisTemplate.keys("*");
//        redisTemplate.delete(keys);
    }

    @Override
    public void delete(String id) {
        mapper.deleteById(id);
//        Set keys = redisTemplate.keys("*");
//        redisTemplate.delete(keys);
    }
}
